/*
 * PlayerPanel.java
 *
 * Created on January 17, 2005, 11:00 PM
 */
package com.bff.mpd.musicjukebox.ui;

import com.bff.mpd.musicjukebox.lib.ColorProperties;
import com.bff.mpd.musicjukebox.lib.JukeboxProperties;
import com.bff.mpd.musicjukebox.lib.actions.MinimizeAction;
import com.bff.mpd.musicjukebox.lib.actions.MuteAction;
import com.bff.mpd.utils.Utils;
import javax.swing.event.ChangeListener;
import com.bff.mpd.musicjukebox.lib.actions.StopAction;
import com.bff.mpd.musicjukebox.lib.actions.PrevAction;
import com.bff.mpd.musicjukebox.lib.actions.PlayAction;
import com.bff.mpd.musicjukebox.lib.actions.PauseAction;
import com.bff.mpd.musicjukebox.lib.actions.NextAction;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.mpd.musicjukebox.ui.components.NextButton;
import com.bff.mpd.musicjukebox.ui.components.PauseButton;
import com.bff.mpd.musicjukebox.ui.components.PlayButton;
import com.bff.mpd.musicjukebox.ui.components.PrevButton;
import com.bff.mpd.musicjukebox.ui.components.ScrollingLabel;
import com.bff.mpd.musicjukebox.ui.components.SongInfoPlayerPanel;
import com.bff.mpd.musicjukebox.ui.components.StopButton;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.ChangeEvent;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.events.PlayerBasicChangeEvent;
import org.bff.javampd.events.PlayerBasicChangeListener;
import org.bff.javampd.events.PlayerChangeEvent;
import org.bff.javampd.events.PlayerChangeListener;
import org.bff.javampd.events.PlaylistBasicChangeEvent;
import org.bff.javampd.events.PlaylistBasicChangeListener;
import org.bff.javampd.events.VolumeChangeEvent;
import org.bff.javampd.events.VolumeChangeListener;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author  Bill
 */
public class PlayerPanelThin extends javax.swing.JPanel
        implements PlayerBasicChangeListener,
        PlayerChangeListener,
        PlaylistBasicChangeListener,
        VolumeChangeListener {

    private Action playAction;
    private Action stopAction;
    private Action pauseAction;
    private Action prevAction;
    private Action nextAction;
    private Action muteAction;
    private Action minimizeAction;
    private long totalTrackTime = 0;
    private MPDPlayer player;
    private boolean muted;
    private Color gradientColor;
    private DetailThread detailThread;

    /** Creates new form PlayerPanel */
    public PlayerPanelThin() {
        super();
        gradientColor = Color.GRAY;
        try {
            setPlayer(JukeboxProperties.getInstance().getPlayer());
            makeActions();
            initComponents();

            volumeSlider.setValue(getPlayer().getVolume());

            JukeboxProperties.getInstance().getMPDMonitor().addPlayerChangeListener(this);
            JukeboxProperties.getInstance().getMPDMonitor().addPlaylistChangeListener(this);
            JukeboxProperties.getInstance().getMPDMonitor().addVolumeChangeListener(this);
            JukeboxProperties.getInstance().getPlayer().addPlayerChangeListener(this);

            volumeSlider.addChangeListener(new ChangeListener() {

                public void stateChanged(ChangeEvent ce) {
                    try {
                        getPlayer().setVolume(volumeSlider.getValue());
                    } catch (MPDConnectionException ex) {
                        Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (MPDPlayerException ex) {
                        Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            songChange();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel5 = new BJJPanel();
        jPanel2 = new BJJPanel();
        btnPause = new PauseButton();
        btnStop = new StopButton();
        btnPlay = new PlayButton();
        btnNext = new NextButton();
        btnPrev = new PrevButton();
        jPanel1 = new SongInfoPlayerPanel();
        jPanel1.setBackground(ColorProperties.PLAYER_INSERT);
        jPanel8 = new BJJPanel();
        jPanel4 = new javax.swing.JPanel();
        timeLabel = new javax.swing.JLabel();
        timeSlider = new javax.swing.JSlider();
        totalTimeLabel = new javax.swing.JLabel();
        titleTextField = new javax.swing.JLabel();
        detailLabel = new ScrollingLabel();
        jPanel3 = new BJJPanel();
        btnMute = new javax.swing.JButton(muteAction);
        btnMute.setText(null);
        btnMute.setContentAreaFilled(false);
        volumeSlider = new javax.swing.JSlider();
        btnVolumeStart = new javax.swing.JButton();
        btnVolumeStart.setText(null);
        btnVolumeStart.setContentAreaFilled(false);
        btnVolumeEnd = new javax.swing.JButton();
        btnVolumeEnd.setText(null);
        btnVolumeEnd.setContentAreaFilled(false);
        jPanel6 = new BJJPanel();
        jPanel7 = new BJJPanel();
        btnMinimize = new javax.swing.JButton(minimizeAction);
        btnMinimize.setText(null);
        btnMinimize.setContentAreaFilled(false);

        setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        setMaximumSize(new java.awt.Dimension(2048, 50));
        setMinimumSize(new java.awt.Dimension(1200, 80));
        setPreferredSize(new java.awt.Dimension(1200, 100));
        setLayout(new java.awt.BorderLayout());

        jPanel5.setMinimumSize(new java.awt.Dimension(800, 100));
        jPanel5.setOpaque(false);
        jPanel5.setPreferredSize(new java.awt.Dimension(1000, 100));
        jPanel5.setLayout(new java.awt.GridBagLayout());

        jPanel2.setMinimumSize(new java.awt.Dimension(208, 70));
        jPanel2.setOpaque(false);
        jPanel2.setPreferredSize(new java.awt.Dimension(208, 70));
        jPanel2.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel2.add(btnPause, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel2.add(btnStop, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        jPanel2.add(btnPlay, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        jPanel2.add(btnNext, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel2.add(btnPrev, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        jPanel5.add(jPanel2, gridBagConstraints);

        jPanel1.setBackground(ColorProperties.PLAYER_INSERT);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setMaximumSize(new java.awt.Dimension(605, 100));
        jPanel1.setMinimumSize(new java.awt.Dimension(605, 100));
        jPanel1.setPreferredSize(new java.awt.Dimension(605, 100));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel8.setBackground(ColorProperties.PLAYER_INSERT);
        jPanel8.setLayout(new java.awt.GridBagLayout());

        jPanel4.setMinimumSize(new java.awt.Dimension(600, 25));
        jPanel4.setOpaque(false);
        jPanel4.setPreferredSize(new java.awt.Dimension(600, 34));
        jPanel4.setLayout(new java.awt.GridBagLayout());

        timeLabel.setBackground(java.awt.Color.lightGray);
        timeLabel.setFont(new java.awt.Font("MS Sans Serif", 0, 18));
        timeLabel.setForeground(java.awt.Color.blue);
        timeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        timeLabel.setText("0:00");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel4.add(timeLabel, gridBagConstraints);

        timeSlider.setBackground(java.awt.Color.lightGray);
        timeSlider.setForeground(java.awt.Color.blue);
        timeSlider.setValue(0);
        timeSlider.setMaximumSize(new java.awt.Dimension(400, 24));
        timeSlider.setMinimumSize(new java.awt.Dimension(200, 25));
        timeSlider.setOpaque(false);
        timeSlider.setPreferredSize(new java.awt.Dimension(400, 24));
        timeSlider.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                timeSliderMouseReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel4.add(timeSlider, gridBagConstraints);

        totalTimeLabel.setBackground(java.awt.Color.lightGray);
        totalTimeLabel.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 18));
        totalTimeLabel.setForeground(new java.awt.Color(51, 51, 255));
        totalTimeLabel.setText("0:00");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        jPanel4.add(totalTimeLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel8.add(jPanel4, gridBagConstraints);

        titleTextField.setBackground(java.awt.Color.lightGray);
        titleTextField.setFont(new java.awt.Font("Tahoma", 1, 14));
        titleTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titleTextField.setText("Artist - Title");
        titleTextField.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 1, 5, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel8.add(titleTextField, gridBagConstraints);

        detailLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        detailLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel8.add(detailLabel, gridBagConstraints);

        jPanel1.add(jPanel8, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel5.add(jPanel1, gridBagConstraints);

        jPanel3.setOpaque(false);
        jPanel3.setLayout(new java.awt.GridBagLayout());

        btnMute.setBackground(java.awt.Color.lightGray);
        btnMute.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd/mute_3.png"))); // NOI18N
        btnMute.setBorder(null);
        btnMute.setMaximumSize(new java.awt.Dimension(50, 50));
        btnMute.setMinimumSize(new java.awt.Dimension(32, 32));
        btnMute.setPreferredSize(new java.awt.Dimension(48, 48));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        jPanel3.add(btnMute, gridBagConstraints);

        volumeSlider.setBackground(java.awt.Color.lightGray);
        volumeSlider.setForeground(java.awt.Color.blue);
        volumeSlider.setMajorTickSpacing(25);
        volumeSlider.setMinorTickSpacing(5);
        volumeSlider.setPaintTicks(true);
        volumeSlider.setValue(100);
        volumeSlider.setMinimumSize(new java.awt.Dimension(100, 25));
        volumeSlider.setOpaque(false);
        volumeSlider.setPreferredSize(new java.awt.Dimension(100, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        jPanel3.add(volumeSlider, gridBagConstraints);

        btnVolumeStart.setBackground(java.awt.Color.lightGray);
        btnVolumeStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd/volume_start.png"))); // NOI18N
        btnVolumeStart.setBorder(null);
        btnVolumeStart.setFocusPainted(false);
        btnVolumeStart.setMaximumSize(new java.awt.Dimension(50, 50));
        btnVolumeStart.setMinimumSize(new java.awt.Dimension(32, 32));
        btnVolumeStart.setPreferredSize(new java.awt.Dimension(32, 32));
        btnVolumeStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolumeStartActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        jPanel3.add(btnVolumeStart, gridBagConstraints);

        btnVolumeEnd.setBackground(java.awt.Color.lightGray);
        btnVolumeEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd/volume_end.png"))); // NOI18N
        btnVolumeEnd.setBorder(null);
        btnVolumeEnd.setFocusPainted(false);
        btnVolumeEnd.setMaximumSize(new java.awt.Dimension(50, 50));
        btnVolumeEnd.setPreferredSize(new java.awt.Dimension(48, 48));
        btnVolumeEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolumeEndActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel3.add(btnVolumeEnd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel5.add(jPanel3, gridBagConstraints);

        add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel6.setOpaque(false);
        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel7.setOpaque(false);

        btnMinimize.setBackground(java.awt.Color.lightGray);
        btnMinimize.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd/minimize.png"))); // NOI18N
        btnMinimize.setBorder(null);
        btnMinimize.setMaximumSize(new java.awt.Dimension(50, 50));
        jPanel7.add(btnMinimize);

        jPanel6.add(jPanel7, java.awt.BorderLayout.SOUTH);

        add(jPanel6, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    private void timeSliderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_timeSliderMouseReleased
        try {
            getPlayer().seek(timeSlider.getValue());
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_timeSliderMouseReleased

    private void btnVolumeStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolumeStartActionPerformed
        try {
            getPlayer().setVolume(volumeSlider.getValue() - 5);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnVolumeStartActionPerformed

    private void btnVolumeEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolumeEndActionPerformed
        try {
            getPlayer().setVolume(volumeSlider.getValue() + 5);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlayerException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnVolumeEndActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMinimize;
    private javax.swing.JButton btnMute;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPause;
    private javax.swing.JButton btnPlay;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnVolumeEnd;
    private javax.swing.JButton btnVolumeStart;
    private javax.swing.JLabel detailLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JSlider timeSlider;
    private javax.swing.JLabel titleTextField;
    private javax.swing.JLabel totalTimeLabel;
    private javax.swing.JSlider volumeSlider;
    // End of variables declaration//GEN-END:variables

    public void makeToolbar(JMenuBar menuBar) {
        makeToolbar(menuBar, -1);
    }

    public void makeToolbar(JMenuBar menuBar, int index) {
        JMenu playMenu = new JMenu("Player");
        playMenu.add(new JMenuItem(prevAction));
        playMenu.add(new JMenuItem(pauseAction));
        playMenu.add(new JMenuItem(stopAction));
        playMenu.add(new JMenuItem(playAction));
        playMenu.add(new JMenuItem(nextAction));
        playMenu.add(new JMenuItem(muteAction));
        menuBar.add(playMenu, index);
    }

    private void makeActions() {
        playAction = new PlayAction(getPlayer());
        stopAction = new StopAction(getPlayer());
        pauseAction = new PauseAction(getPlayer());
        prevAction = new PrevAction(getPlayer());
        nextAction = new NextAction(getPlayer());
        muteAction = new MuteAction(getPlayer());
        minimizeAction = new MinimizeAction();
    }

    protected JFrame getParentComponent(Component component) {
        if (component instanceof JPopupMenu) {
            component = ((JPopupMenu) component).getInvoker();
        }
        Component parent = component.getParent();
        if (parent instanceof JPopupMenu) {
            component = ((JPopupMenu) parent).getInvoker();
            parent = component.getParent();
        }
        while (!(parent instanceof JFrame) && (parent != null)) {
            parent = parent.getParent();
        }

        return (JFrame) parent;
    }

    public void setElapsedTrackTime(long seconds) {
        if (seconds <= totalTrackTime) {
            timeLabel.setText(Utils.formatTime(seconds));
            timeSlider.setValue((int) seconds);
        }
    }

    public void setTotalTrackTime(long seconds) {
        totalTrackTime = seconds;
        totalTimeLabel.setText(Utils.formatTime(seconds));
        timeSlider.setMaximum((int) seconds);
    }

    @Override
    public void playerBasicChange(PlayerBasicChangeEvent event) {
        switch (event.getId()) {
            case (PlayerBasicChangeEvent.PLAYER_STOPPED):

            case (PlayerBasicChangeEvent.PLAYER_STARTED):
                songChange();
                break;
            case (PlayerBasicChangeEvent.PLAYER_PAUSED):
                break;
            case (PlayerBasicChangeEvent.PLAYER_UNPAUSED):
                break;


            default:
                break;
        }
    }

    @Override
    public void playerChanged(PlayerChangeEvent event) {
        switch (event.getId()) {
//            case(PlayerChangeEvent.PLAYER_ERROR):
//                titleTextField.setText(" ");
//                setElapsedTrackTime(0);
//                break;
            case (PlayerChangeEvent.PLAYER_NEXT):
                songChange();
                break;
            case (PlayerChangeEvent.PLAYER_PREVIOUS):
                break;
            case (PlayerChangeEvent.PLAYER_MUTED):
                volumeSlider.setEnabled(false);
                //jLabel1.setEnabled(false);
                break;
            case (PlayerChangeEvent.PLAYER_UNMUTED):
                volumeSlider.setEnabled(true);
                //jLabel1.setEnabled(true);
                break;
        }
    }

    @Override
    public void playlistBasicChange(PlaylistBasicChangeEvent event) {
        switch (event.getId()) {
            case PlaylistBasicChangeEvent.SONG_CHANGED:
                songChange();
                break;
        }
    }

    protected void songChange() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    volumeSlider.setValue(getPlayer().getVolume());
                    MPDSong song = getPlayer().getCurrentSong();

                    if (isPlaying()) {
//                        statusLabel.setText("Playing");

                        if (detailThread != null) {
                            detailThread.setStopped(true);
                        }
                        detailThread = new DetailThread(song);
                        new Thread(detailThread).start();

                    }

                    if (song != null) {
                        setTotalTrackTime(song.getLength());
                        titleTextField.setText(song.getTitle());
                    } else {
                        setTotalTrackTime(0);
                        titleTextField.setText("");
                    }
                } catch (Exception ex) {
                    Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();
    }

    private boolean isPlaying() {
        try {
            return getPlayer().getStatus() == MPDPlayer.PlayerStatus.STATUS_PLAYING;
        } catch (MPDException ex) {
            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    //to force song change when above events arent enough
    public void setSong(MPDSong song) {
        if (song != null) {
            setTotalTrackTime(song.getLength());
            titleTextField.setText(song.getArtist() + "-" + song.getTitle());
        }
    }

    public void paintComponent(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Create the gradient paint
        int blue = getBackground().getBlue();
        GradientPaint paint = new GradientPaint(0, 0, getBackground(), width, height, gradientColor, true);

        // we need to cast to Graphics2D for this operation
        Graphics2D g2d = (Graphics2D) g;

        // save the old paint
        Paint oldPaint = g2d.getPaint();

        // set the paint to use for this operation
        g2d.setPaint(paint);
        // fill the background using the paint
        g2d.fillRect(0, 0, width, height);
        // restore the original paint
        g2d.setPaint(oldPaint);
    }

    /**
     * @return the player
     */
    public MPDPlayer getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(MPDPlayer player) {
        this.player = player;
    }

    public void volumeChanged(VolumeChangeEvent event) {
        volumeSlider.setValue(event.getVolume());
    }

    private class DetailThread implements Runnable {

        private boolean stopped;
        private final MPDSong song;
        private ScrollingLabel scrollingLabel;

        public DetailThread(MPDSong song) {
            this.song = song;
            scrollingLabel = (ScrollingLabel) detailLabel;
        }

        @Override
        public void run() {
            while (!isStopped()) {

                if (!isStopped()) {
                    if (song.getArtist() != null) {
                        scrollingLabel.scrollTheText(song.getArtist().getName());
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

                if (song.getAlbum() != null) {

                    if (!isStopped()) {
                        scrollingLabel.scrollTheText(song.getAlbum().getName());

                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

                if (!isStopped()) {
                    if (song.getYear() != null && !"".equals(song.getYear()) && !"0".equals(song.getYear())) {
                        scrollingLabel.scrollTheText(song.getYear());
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (!isStopped()) {
                    if (song.getGenre() != null
                            && !"".equals(song.getGenre())
                            && !"No Genre".equals(song.getGenre())) {
                        scrollingLabel.scrollTheText(song.getGenre());
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(PlayerPanelThin.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (!isStopped()) {
                    if (song.getComment() != null && !"".equals(song.getComment())) {
                        scrollingLabel.scrollTheText(song.getComment());
                    }
                }
            }

            scrollingLabel.setText(" ");
        }

        public boolean isStopped() {
            return stopped;
        }

        public void setStopped(boolean stopped) {
            this.stopped = stopped;
        }

        public MPDSong getSong() {
            return song;
        }
    }
}
